package daoimpl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Repository;

import pojo.Offer;
import pojo.Overview;
import pojo.Product;
import pojo.Specification;
import dao.OfferDAO;

@Repository("OfferDAO")
public class OfferDAOImpl implements OfferDAO {

	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public List<Offer> getListOffer() {
		List<Offer> of = null;
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
        	String hql = "Select offer from Offer offer";
            Query query = session.createQuery(hql);
            of = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return of;
	}

	public List<Offer> getListOffer(int offset, int num) {
		List<Offer> of = null;
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
            String hql = "Select offer from Offer offer";
            
            Query query = session.createQuery(hql);
            query.setFirstResult( offset);
            query.setMaxResults( num );
            
            of = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return of;
	}
	
	public List<Offer> getListBanner(int offset, int num)
	{
		List<Offer> of = null;
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
            String hql = "Select offer from Offer offer where imageBanner!=NULL and imageBanner!='' and offer.enable=1";
            
            Query query = session.createQuery(hql);
            query.setFirstResult( offset);
            query.setMaxResults( num );
            
            of = query.list();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return of;
	}

	public Offer getOfferFromId(int id) {
		Offer offer = null;
		
		Session session = null;
		try{
			session = sessionFactory.getCurrentSession();
		}
		catch(HibernateException ex)
		{
			session = sessionFactory.openSession();
		}
			
        try {
        	String hql = "Select off from Offer off where off.id=:id";
            Query query = session.createQuery(hql);
            query.setParameter("id", id);
            offer = (Offer)query.uniqueResult();
        } catch (HibernateException ex) {
            System.err.println(ex);
        } finally {
        	session.close();
        }
        return offer;
	}

	public boolean addOffer(Offer offer, boolean enable){
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			offer.setEnable(enable);
			
			session.save(offer);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}
	
	public boolean updateOffer(Offer offer, boolean enable){
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			offer.setEnable(enable);
			
			session.saveOrUpdate(offer);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}
	
	public boolean deleteOffer(Offer offer) {
		Session session = null;
		try {
			session = sessionFactory.getCurrentSession();
		} catch (HibernateException ex) {
			session = sessionFactory.openSession();
		}

		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			
			session.delete(offer);
			transaction.commit();
		} catch (HibernateException ex) {
			transaction.rollback();
			System.err.println(ex);
			return false;
		} finally {
			session.close();
		}
		return true;
	}
}
